import java.util.Arrays;

public class removeElement {
    public static int removeElement(int[] nums, int val) {
        int size = nums.length;
        System.out.println("覆盖前:" + Arrays.toString(nums) + "\n");
        for(int i = 0 ; i < size ; ){
            if(nums[i] == val){
                for(int j = i ; j < size - i - 1; j++){
                    nums[j] = nums[j+1];
                }
                System.out.println("覆盖后:" + Arrays.toString(nums) + "\n");
                System.out.println("size : " + size + " i : " + i);

                size--;
                System.out.println("删除一个元素");
            }
            // 防止循环连续出现 val
            if(nums[i] != val){
                i++;
            }
        }
        return size;
    }

    public static void main(String[] args) {
        // 测试用例
        int[] nums = {0, 1, 2, 2, 3, 0, 4, 2};
        int val = 2;

        // 打印原始数组
        System.out.println("原始数组: " + Arrays.toString(nums));
        System.out.println("要移除的值: " + val);

        // 调用方法
        int newSize = removeElement(nums, val);

    }
}
